package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.message.pojo.vo.MessageRabbitMqVO;
import com.xbongbong.pro.product.pojo.dto.CostRecalculationDTO;
import com.xbongbong.saas.service.CostRecalculationService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author jiping.yang
 * @date 2021/12/2 18:37
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/message/mq/costRecalculation")
public class CostRecalculationController {

    @Resource
    private CostRecalculationService costRecalculationService;

    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String costRecalculationAdd(@RequestBody @Valid CostRecalculationDTO costRecalculationDTO, BindingResult br) throws Exception{
        XbbResponse<MessageRabbitMqVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                costRecalculationService.costRecalculationAdd(costRecalculationDTO);
                MessageRabbitMqVO messageRabbitMqVO = new MessageRabbitMqVO();
                response = new XbbResponse<>(messageRabbitMqVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String costRecalculationDelete(@RequestBody @Valid CostRecalculationDTO costRecalculationDTO, BindingResult br) throws Exception{
        XbbResponse<MessageRabbitMqVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                costRecalculationService.costRecalculationDelete(costRecalculationDTO);
                MessageRabbitMqVO messageRabbitMqVO = new MessageRabbitMqVO();
                response = new XbbResponse<>(messageRabbitMqVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
